﻿package temp
{
    import com.sqstudio.study.util.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
	import util.Bezier;

    public class ArcherStudy extends Sprite
    {
        private var arrow:UI_Arrow;
        private var drawPathSp:Sprite;
        private var startPoint:Point;
        private var endPoint:Point;
        private var controlPoint:Point;
        private var tower:UI_Tower;
        private var monster:UI_Monster;
        private var steps:uint;
        private var crtStep:int;

        public function ArcherStudy()
        {
            this.tower = new UI_Tower();
            this.tower.shooterLeft.stop();
            this.tower.shooterRight.stop();
            this.addChild(this.tower);
            this.tower.x = 100;
            this.tower.y = 150;
            this.monster = new UI_Monster();
            this.addChild(this.monster);
            this.monster.x = 400;
            this.monster.y = 300;
            this.arrow = new UI_Arrow();
            this.addChild(this.arrow);
            this.startPoint = new Point(this.tower.x, this.tower.y - 30);
            this.controlPoint = new Point(this.monster.x - 200, this.tower.y - 130);
            this.endPoint = new Point(this.monster.x, this.monster.y - 20);
            this.steps = Bezier.init(this.startPoint, this.controlPoint, this.endPoint, 8);
            this.crtStep = 0;
            this.addEventListener(Event.ENTER_FRAME, this.loopEfHandler);
            this.drawData();
            return;
        }// end function

        private function loopEfHandler(event:Event) : void
        {
            var _loc_2:* = Bezier.getAnchorPoint(this.crtStep);
            this.arrow.x = _loc_2[0];
            this.arrow.y = _loc_2[1];
            this.arrow.rotation = _loc_2[2];
            var _loc_3:* = this;
            var _loc_4:* = this.crtStep + 1;
            _loc_3.crtStep = _loc_4;
            if (this.crtStep > this.steps)
            {
                this.crtStep = 0;
                if (Math.random() > 0.5)
                {
                    this.tower.shooterLeft.gotoAndPlay(2);
                }
                else
                {
                    this.tower.shooterRight.gotoAndPlay(2);
                }
            }
            return;
        }// end function

        private function drawData() : void
        {
            this.drawPathSp = new Sprite();
            this.addChild(this.drawPathSp);
            this.drawPathSp.graphics.lineStyle(1, 0, 0.5);
            this.drawPathSp.graphics.moveTo(this.startPoint.x, this.startPoint.y);
            this.drawPathSp.graphics.curveTo(this.controlPoint.x, this.controlPoint.y, this.endPoint.x, this.endPoint.y);
            this.drawPathSp.graphics.endFill();
            this.drawP(this.startPoint);
            this.drawP(this.endPoint);
            this.drawP(this.controlPoint);
            this.drawPathSp.graphics.moveTo(this.startPoint.x, this.startPoint.y);
            this.drawPathSp.graphics.lineTo(this.endPoint.x, this.endPoint.y);
            this.drawPathSp.graphics.lineTo(this.controlPoint.x, this.controlPoint.y);
            this.drawPathSp.graphics.lineTo(this.startPoint.x, this.startPoint.y);
            return;
        }// end function

        public function drawP(p:Point) : Sprite
        {
            var _loc_2:* = new Sprite();
            this.addChild(_loc_2);
            _loc_2.graphics.beginFill(16777215 * Math.random());
            _loc_2.graphics.drawCircle(0, 0, 4);
            _loc_2.graphics.endFill();
            _loc_2.x = p.x;
            _loc_2.y = p.y;
            return _loc_2;
        }// end function

    }
}
